﻿(function () {
    $(function () {

        var _$dataTable = $('#DataTable');
        var _dataService = abp.services.app.outbound;

        var _permissions = {
            create: abp.auth.hasPermission('Pages.Inventory.Outbounds.Create'),
            edit: abp.auth.hasPermission('Pages.Inventory.Outbounds.Edit'),
            'delete': abp.auth.hasPermission('Pages.Inventory.Outbounds.Delete')
        };

        var _createOrEditModal = new app.ModalManager({
            viewUrl: abp.appPath + 'Inventory/Outbounds/CreateOrEditModal',
            scriptUrl: abp.appPath + 'Areas/Inventory/Views/Outbounds/_CreateOrEditModal.js',
            modalClass: 'CreateOrEditModal'//类名跟_CreateOrEditModal.js文件里面的类名app.modals.CreateOrEditModal一致，就能调用模态窗口了
        });
        //导入界面
        var _importModal = new app.ModalManager({
            viewUrl: abp.appPath + 'Inventory/Outbounds/ImportModal',
            scriptUrl: abp.appPath + 'Areas/Inventory/Views/Outbounds/_ImportModal.js',
            modalClass: 'ImportModal'
        });
        _$dataTable.jtable({

            title: app.localize('Menu_Inventory.Outbounds'),
            paging: true,
            sorting: true,
            multiSorting: true,

            actions: {
                listAction: {
                    method: _dataService.getObjects
                }
            },

            fields: {
                id: {
                    key: true,
                    list: false
                },
                actions: {
                    title: app.localize('Actions'),
                    width: '10%',
                    sorting: false,
                    list: _permissions.edit || _permissions.delete,
                    display: function (data) {
                        var $span = $('<span></span>');

                        if (_permissions.edit) {
                            $('<button class="btn btn-default btn-xs" title="' + app.localize('Edit') + '"><i class="fa fa-edit"></i></button>')
                                .appendTo($span)
                                .click(function () {
                                    _createOrEditModal.open({ id: data.record.id });
                                });
                        }

                        if (_permissions.delete) {
                            $('<button class="btn btn-default btn-xs" title="' + app.localize('Delete') + '"><i class="fa fa-trash-o"></i></button>')
                                .appendTo($span)
                                .click(function () {
                                    deleteObject(data.record);
                                });
                        }

                        return $span;
                    }
                },
                vendor: {
                    title: app.localize('Page_Inventory.Outbounds_Column_Vendor'),
                    width: '100'
                },
                invoiceNumber: {
                    title: app.localize('Page_Inventory.Outbounds_Column_InvoiceNumber'),
                    width: '20%'
                },
                materialNo: {
                    title: app.localize('Page_Inventory.Outbounds_Column_MaterialNo'),
                    width: '20%'
                },
                materialNameEn: {
                    title: app.localize('Page_Inventory.Outbounds_Column_MaterialNameEn'),
                    width: '20%'
                },
                materialNameCn: {
                    title: app.localize('Page_Inventory.Outbounds_Column_MaterialNameCn'),
                    width: '20%'
                },
                qty: {
                    title: app.localize('Page_Inventory.Outbounds_Column_Qty'),
                    width: '10%'
                },
                netWeight: {
                    title: app.localize('Page_Inventory.Outbounds_Column_NetWeight'),
                    width: '10%'
                },
                grossWeight: {
                    title: app.localize('Page_Inventory.Outbounds_Column_GrossWeight'),
                    width: '10%'
                },
                creationTime: {
                    title: app.localize('Page_Inventory.Outbounds_Column_CreationTime'),
                    width: '20%'
                }
            }

        });

        function getObjects(reload) {
            if (reload) {
                _$dataTable.jtable('reload');
            } else {
                _$dataTable.jtable('load', {
                    filter: $('#DataTableFilter').val()
                });
            }
        }

        function deleteObject(o) {
            abp.message.confirm(
                app.localize('Com_Msg_Delete_Warning', o.name),
                function (isConfirmed) {
                    if (isConfirmed) {
                        _dataService.deleteObject({
                            id: o.id
                        }).done(function () {
                            getObjects(true);
                            abp.notify.success(app.localize('SuccessfullyDeleted'));
                        });
                    }
                }
            );
        }

        $('#CreateNewButton').click(function () {
            _createOrEditModal.open();
        });

        $('#ExportToExcelButton').click(function () {
            _dataService
                .getObjectsToExcel({
                    filter: $('#DataTableFilter').val()
                })
                .done(function (result) {
                    app.downloadTempFile(result);
                });
        });
        //导入按钮
        $('#ImportButton').click(function () {
            _importModal.open();
        });
        $('#GetListButton').click(function (e) {
            e.preventDefault();
            getObjects();
        });

        abp.event.on('app.CreateOrEditModalSaved', function () {
            getObjects(true);
        });
        //导入完成刷新父窗体
        abp.event.on('app.ImportModalSaved', function () {
            getObjects(true);
        });
        getObjects();

        $('#DataTableFilter').focus();
    });
})();